package com.zerosoft.zerovideo.service;

import com.zerosoft.zerovideo.config.ResourceProperties;
import com.zerosoft.zerovideo.utils.RedisUtil;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.nio.file.StandardOpenOption;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

@Service
@Data
@Slf4j
public class VideoResourceService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private ResourceProperties resourceProperties;

    @Value("${spring.application.name}")
    private String appName;

    final private String resourceKey = "video-resources";

    /**
     * 初始化资源，从当前目录下获取所有视频资源，并保存到redis中
     */
    @PostConstruct
    public void initResources() {
        getResourceFiles();
    }

    private void getResourceFiles() {
        Path currentDir = Paths.get(".", resourceProperties.getPath());
        System.out.println("Current directory: " + currentDir.toAbsolutePath());

//        try{
//
//            // 列出当前目录下的所有文件和子目录
//            Files.list(currentDir)
//                    .forEach(path -> {
//                        System.out.printf(path.getFileName().toString());
//                        System.out.println(path.toString());
//                    });
//
//            // 如果你只想列出子目录，可以使用过滤器
//            Files.list(currentDir)
//                    .filter(Files::isDirectory)
//                    .forEach(System.out::println);
//
//            // 如果你只想列出文件，同样可以使用过滤器
//            Files.list(currentDir)
//                    .filter(path -> !Files.isDirectory(path))
//                    .forEach(System.out::println);
//        }
//        catch (Exception e){
//            log.error("获取资源失败", e);
//        }

        HashSet<String> set = getPathFiles(currentDir);
//        redisUtil.sSet(appName+":"+resourceKey, set);
        String key = appName+":"+resourceKey;
        set.forEach(item->{
            System.out.println("Files:" + item);
//            redisUtil.sSet(resourceProperties.getExt(), item);
            redisUtil.sSet(key, item);
        });
        redisUtil.expire(key, resourceProperties.getExpire());
        Set obj =redisUtil.sGetRandom(key, 3);

        obj.forEach(item->{
            System.out.println("Files-----" + item);
        });
        String s = "/Users/guxuefeng/Downloads/反诈短剧-黑喵警长.mp4";
        System.out.println("BaseEncode: "+s+"==> " + String.valueOf(Base64.getEncoder().encodeToString(s.getBytes())));
    }
    @SneakyThrows
    private HashSet<String> getPathFiles(Path path)
    {
        HashSet<String> set = new HashSet<String>();
        Files.list(path)
                .filter(item -> !Files.isDirectory(item))
                .forEach(file->{
                    System.out.printf(file.toString());
                    set.add(file.toString());
                });
        Files.list(path)
            .filter(Files::isDirectory)
            .forEach(item -> {
                System.out.printf(item.toString());
                set.addAll(getPathFiles(item));
             });
        return set;
    }

    public Set getResources() {
        String key = appName+":"+resourceKey;
        if (!redisUtil.hasKey(key) || redisUtil.sGetSetSize(key) == 0) {
            getResourceFiles();
        }
        return redisUtil.sGetRandom(key, resourceProperties.getNumsPerPage());
    }

    /**
     * 根据媒体文件，获取视频文件封面
     * @param path
     * @return
     */
    @SneakyThrows
    public byte[] getCover(Path path) {

        return null;
    }
}
